package com.sen.rbac.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sen.common.common.bo.SysAccountBO;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.response.PageVo;
import com.sen.common.common.response.ResultMsgEnum;
import com.sen.common.common.util.CheckUtils;
import com.sen.rbac.constant.RbacConstant;
import com.sen.rbac.dto.SysAccountDto;
import com.sen.rbac.enums.RbacMsgEnum;
import com.sen.rbac.exception.RbacException;
import com.sen.rbac.feign.RbacAccountFeign;
import com.sen.rbac.service.AccountService;
import com.sen.rbac.vo.SysAccountVO;
import com.sen.rbac.vo.SysRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.sen.rbac.enums.RbacMsgEnum.*;


/**
 * @author esjiang
 */
@RestController
@RequestMapping("/account")
@Slf4j
public class AccountController extends BaseController implements RbacAccountFeign {

    @Resource
    AccountService accountService ;
    /**
     * 根据账户名查询账户信息
     * @param userName
     * @param tenantCode
     * @param systemCode
     * @return
     */
    @Override
    @RequestMapping("/selectAccountByUserNameForGateway")
    public MessageResult<SysAccountBO> selectAccountByUserNameForGateway(String userName, String tenantCode, String systemCode) {
        return exec("根据用户名查询用户信息",userName,()->{
            CheckUtils.checkNotNull(userName);
            SysAccountBO sysAccountBO = accountService.selectAccountByUserName(userName,tenantCode,systemCode);
            return MessageResult.success(sysAccountBO) ;
        }) ;
    }

    /**
     * 新增
     * @param accountDto
     * @return
     */
    @Override
    @PostMapping("/add")
    public MessageResult add(@RequestBody SysAccountDto accountDto){
        return exec("新增账户信息",accountDto,()->{
            CheckUtils.checkNotNull(accountDto,"userName","password");
            if(StrUtil.isBlank(accountDto.getUserType())){
                accountDto.setUserType(RbacConstant.user_type_4);
            }
            if (accountService.addAccount(accountDto)){
                return MessageResult.success(null) ;
            }else {
                return MessageResult.messageResult(null, ResultMsgEnum.SUCCESS, ACCOUNT_ADD_FAILED) ;
            }
        }) ;
    }

    /**
     * 更新账户信息
     * @param accountDto
     * @return
     */
    @PostMapping("/update")
    public MessageResult update(@RequestBody SysAccountDto accountDto){
        return exec("更新账户信息",accountDto,()->{
            if (accountDto == null || accountDto.getId() == 0){
                return MessageResult.messageResult(null,ResultMsgEnum.SUCCESS,ResultMsgEnum.PARAM_IS_NULL);
            }
            if (accountService.updateAccount(accountDto)){
                return MessageResult.success(null) ;
            } else {
                return MessageResult.messageResult(null,ResultMsgEnum.SUCCESS, RbacMsgEnum.ACCOUNT_UPDATE_FAILED) ;
            }
        }) ;
    }

    /**
     * 删除账户
     * @param userName
     * @return
     */
    @Override
    @GetMapping("/del")
    public MessageResult delete(@RequestParam("userName") String userName){
        return exec("删除账户",userName,()->{
            CheckUtils.checkNotNull(userName);
            if (accountService.deleteAccount(userName)) {
                return MessageResult.success(null) ;
            }else {
                return MessageResult.bizFailed(RbacMsgEnum.ACCOUNT_DEL_FAILED) ;
            }
        }) ;
    }

    /**
     * 列表查询
     * @param accountDto
     * @return
     */
    @Override
    @PostMapping("/list")
    public MessageResult list(@RequestBody SysAccountDto accountDto){
        return exec("查询账户列表",accountDto ,()->{
            if (accountDto == null){
                return MessageResult.bizFailed(ResultMsgEnum.PARAM_IS_NULL) ;
            }
            PageVo<SysAccountVO> sysAccountVOPageVo = accountService.list(accountDto);
            return MessageResult.success(sysAccountVOPageVo) ;
        }) ;
    }

    /**
     * 重置密码
     * @param userName
     * @return
     */
    @GetMapping("/reset")
    public MessageResult resetPassword(String userName){
        return exec("重置密码",userName,()->{
           if (StrUtil.isBlank(userName)){
               return MessageResult.messageResult(null,ResultMsgEnum.SUCCESS,ResultMsgEnum.PARAM_IS_NULL) ;
           }
            try {
                if (accountService.resetPassword(userName)) {
                    return MessageResult.success(null) ;
                }
            } catch (RbacException e) {
                log.error(Convert.toStr(e.getRespCodeEnum().getCode()),e);
                return MessageResult.bizFailed(e.getRespCodeEnum()) ;
            }
            return MessageResult.bizFailed(ACCOUNT_RESET_FAILED);
        });
    }

    /**
     * 检查账户名唯一性
     * @param userName
     * @return
     */
    @Override
    @GetMapping("/checkUniqueAccount")
    public MessageResult checkUniqueAccount(@RequestParam("userName") String userName){
        return exec("检查账户名唯一性",userName,()->{
            CheckUtils.checkNotNull(userName);
            if (accountService.checkUniqueAccount(userName)) {
                return MessageResult.success(null) ;
            }
            return MessageResult.messageResult(null,ResultMsgEnum.SUCCESS,ACCOUNT_DUPLICATE_ERROR);
        });
    }

    /**
     * 启用停用账户
     * @param sysAccountDto
     * @return
     */
    @PostMapping("/enable")
    public MessageResult enableAccount(@RequestBody SysAccountDto sysAccountDto){
        return exec("启用停用账户",sysAccountDto,()->{
            CheckUtils.checkNotNull(sysAccountDto,"enable","userName");
            try {
                boolean b = accountService.enableAccount(sysAccountDto.getUserName(), sysAccountDto.getEnable());
                if (b){
                    return MessageResult.success(null);
                }
            } catch (RbacException e) {
                log.error(e.getRespCodeEnum().toString(),e);
                return MessageResult.bizFailed(e.getRespCodeEnum());
            }
            return MessageResult.bizFailed(ACCOUNT_ENABLE_FAILED);
        }) ;
    }

    /**
     * todo
     * 查询账户所拥有的角色
     * @param userName
     * @return
     */
    @GetMapping("/roles")
    public MessageResult getRolesByUserName(String userName){
        return exec("查询用户的角色",userName,()->{
            CheckUtils.checkNotNull(userName);
            List<SysRoleVO> rolesByUserName = accountService.getRolesByUserName(userName);
            return MessageResult.success(rolesByUserName);
        });
    }

    /**
     * 账户关联角色
     * @param sysAccountDto
     * @return
     */
    @PostMapping("/relatedRoles")
    public MessageResult relatedRoles(@RequestBody SysAccountDto sysAccountDto){
        return exec("账户关联角色",sysAccountDto,()->{
            CheckUtils.checkNotNull(sysAccountDto,"userName");
            boolean b = accountService.relatedRole(sysAccountDto.getUserName(), sysAccountDto.getRoles());
            if (b){
                return MessageResult.success();
            }
            return MessageResult.bizFailed(account_related_role_failed);
        });
    }

    /**
     * 修改密码
     * @param sysAccountDto
     * @return
     */
    @PostMapping("/updatePassword")
    public MessageResult updatePassword(@RequestBody SysAccountDto sysAccountDto){
        return exec("修改密码",sysAccountDto,()->{
            CheckUtils.checkNotNull(sysAccountDto,"password","userName","newPassword");
            if (accountService.updatePassword(sysAccountDto)) {
                return MessageResult.success();
            }
            return MessageResult.bizFailed(update_password_failed);
        });
    }


    /**
     * 根据账户名查询账户信息
     * @param userName
     * @return
     */
    @Override
    @GetMapping("/selectAccountByUserName")
    public MessageResult<SysAccountBO> selectAccountByUserName(@RequestParam("userName")  String userName) {
        return exec("根据用户名查询用户信息",userName,()->{
            CheckUtils.checkNotNull(userName);
            SysAccountBO sysAccountBO = accountService.selectAccountByUserName(userName);
            return MessageResult.success(sysAccountBO) ;
        }) ;
    }

}
